package com.ybbase.framework.config.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ybbase.framework.model.po.system.Organization;
import com.ybbase.framework.model.po.system.User;
import com.ybbase.framework.service.system.OrganizationService;
import com.ybbase.framework.service.system.UserService;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author cg
 * @date 2019-11-20 19:11
 * @since 4.0
 */
@Component
public class MetMetaObjectHandler implements MetaObjectHandler {


    /**
     * 新增时填充
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        System.out.println("insertFill......");
        // createTime 对应实体属性名, 第二个参数需要填充的值
        setInsertFieldValByName("createDate", new Date(), metaObject);
        setInsertFieldValByName("updateTime", new Date(), metaObject);
        if(user!=null){
            setInsertFieldValByName("organId",user.getOrganId(),metaObject);
            setInsertFieldValByName("createBy", user.getId(), metaObject);
            setInsertFieldValByName("updateBy", user.getId(), metaObject);
        }
    }

    /**
     * 修改时填充
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        System.out.println("updateFill......");
        // updateTime 对应实体属性名, 第二个参数需要填充的值
        setUpdateFieldValByName("updateTime", new Date(), metaObject);
        if(user!=null){
            setUpdateFieldValByName("updateBy", user.getId(), metaObject);
        }
    }


}
